package com.lujieni.springbootwebsocket.config;


import org.springframework.amqp.core.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author
 * @Title: ExchangeAndQueueConfig
 * @Description:
 * @date 2022/6/16 9:27 下午
 *
 * 一个很普通的交换机到队列的方式
 * createTraceExchange=>createTraceQueue
 */
@Configuration
public class WebsocketMqConfig implements EnvironmentAware {

    private Environment env;

    @Bean
    public FanoutExchange createWebsocketExchange(){
        return new FanoutExchange("websocket-fanout-exchange", true, false);
    }

    @Bean
    public Queue createWebsocketQueue() throws UnknownHostException {
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        return new Queue(ip+"-"+port+"-queue", true);
    }

    @Bean
    public Binding createBinding() throws UnknownHostException {
        return BindingBuilder.bind(createWebsocketQueue()).to(createWebsocketExchange());
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }





}
